package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.AlquilerCanchaDao;
import edu.upc.clase.demo.entity.Alquiler;
import java.util.Date;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Omar
 */
@Repository
public class AlquilerCanchaDaoImpl extends SimpleJdbcDaoSupport implements AlquilerCanchaDao {

    private static Logger log = LoggerFactory.getLogger(AlquilerCanchaDaoImpl.class);

    @Autowired
    public AlquilerCanchaDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }

    @Override
    public Integer insertar(Alquiler alquiler) {

        getJdbcTemplate().update("insert into ZALQCANCH(IDCAN,IDLOCAL,FECRESER,HHINI,HHFIN,TOTALQU) values (?,?,?,?,?,?)",
                alquiler.getIDCAN(), alquiler.getIDLOCAL(), alquiler.getFECRESER(), alquiler.getHHINI(),
                alquiler.getHHFIN(), alquiler.getTOTALQU());
        return getSimpleJdbcTemplate().queryForInt("call identity()");
    }

    @Override
    public Alquiler buscarporId(Alquiler alquiler) {
        try {
            String Select = "select NROITEM from ZALQCANCH where nroitem = '" + alquiler.getNROITEM() + "'";
            return getSimpleJdbcTemplate().queryForObject(Select,
                    new BeanPropertyRowMapper<Alquiler>(Alquiler.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<Alquiler> listar(Alquiler alquiler, Date fechaInicio, Date fechaFinal) {
        try {
            String Select = "select NROITEM,IDCAN,IDLOCAL,FECRESER,HHINI,HHFIN,TOTALQU from ZALQCANCH"
                    + "where IDLOCAL = '" + alquiler.getIDLOCAL() + "' and "
                    + "(FECRESER >= " + fechaInicio + " FECRESER <= " + fechaFinal + " )";

            return getSimpleJdbcTemplate().query(Select,
                        new BeanPropertyRowMapper<Alquiler>(Alquiler.class));

        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public Alquiler buscarporFechaLocal(Alquiler alquiler) {
        try {
            String Select = "select NROITEM,IDCAN,IDLOCAL,FECRESER,HHINI,HHFIN,TOTALQU from ZALQCANCH"
                    + "where IDLOCAL = ? and HHINI = ? and HHFIN = ?";

            return (Alquiler) getSimpleJdbcTemplate().query(Select,
                        new BeanPropertyRowMapper<Alquiler>(Alquiler.class),alquiler.getIDLOCAL(),
                        alquiler.getHHINI(), alquiler.getHHFIN());

        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
